<template>
	<view>
		<view style="margin: 10rpx;">
			<form @submit="formSubmit" @reset="formReset">
				<view style="height: 100rpx;margin-top: 30rpx;">
					<textarea @blur="bindTextAreaBlur" auto-height placeholder="输入书名" maxlength="36" name="name" v-model="name"/>
				</view>
				<hr>
				<view style="height: 100rpx;margin-top: 30rpx;">
					<textarea @blur="bindTextAreaBlur" auto-height placeholder="输入作者" maxlength="36" name="author" v-model="author"/>
				</view>
				<hr>
				<view style="min-height: 100rpx;width: 100%;margin-top: 30rpx;">
					<textarea v-model="desc" @blur="bindTextAreaBlur" auto-height placeholder="输入书简介" maxlength="-1" style="width: 100%;" name="desc"/>
				</view>
				<hr>
				<view style="margin: 30rpx;">
					<uni-file-picker limit="1" auto-upload="true" @select="uploadFile" title="封面" v-model="selectImage"></uni-file-picker>
				</view>
				<view style="margin-bottom: 30rpx;">
					<uni-data-select name="type"
					  empty-text="请选择书籍分类" v-model="selectCategory" 
					  :localdata="range"
					  placeholder="请选择书籍分类"
					  @change="change"
					></uni-data-select>
				</view>
				<button type="default" plain="true" form-type="submit" >确认</button>
			</form>
			
		</view>
		
	</view>
</template>

<script setup>
import {onLoad,onShow} from "@dcloudio/uni-app"
import { ref } from 'vue';
import {baseUrl} from '../../static/js/util.js'

	let range=ref([])
	let userInfo={}
	let selectCategory=ref(0)
	let imageId=0;
	let name=ref('')
	let desc=ref('')
	let selectImage=ref([])
	
	let bookId=0;
	
	function formSubmit(e){
		if(selectCategory.value===0){
			uni.showToast({
				icon:'fail',
				title:'请先选择分类'
			})
			return 
		}
		if(imageId===0){
			uni.showToast({
				icon:'fail',
				title:'请添加一个封面'
			})
			return 
		}
		e.detail.value.userId=JSON.parse(userInfo).id
		e.detail.value.imageId=imageId
		e.detail.value.categoryId=selectCategory.value
		
		if(bookId!=null &&bookId!=0){
			e.detail.value.id=bookId
		}
		console.log(e.detail.value)
		let authorization=localStorage.getItem("Authorization")
		uni.request({
			url:baseUrl+"/book/saveOrUpdate",
			method:'POST',
			header:{'Authorization':authorization},
			data:e.detail.value,
			success:res=>{
				uni.showToast({
					icon:res.data.code=='200'?'success':'error',
					title:res.data.msg
				})
				console.log(res.data)
				if(res.data.code=='200'){
					uni.showToast({
						icon:'success',
						title:'上传成功，请等待审核'
					})
					uni.switchTab({
						url:'/pages/index/index'
					})
				}
			}
		})
	}
	
	function uploadFile(e){
		let authorization=localStorage.getItem("Authorization")
		// console.log(e)
		uni.uploadFile({
			url:baseUrl+"/file/uploadImage",
			method:'POST',
			name:"file",
			header:{'Authorization':authorization},
			filePath:e.tempFilePaths[0],
			success:res=>{
				imageId=JSON.parse(res.data).data
			},
			fail(f) {
				console.log(f)
			}
		})
		
	}
	
	onLoad((option)=>{
		uni.request({
			url:baseUrl+"/category/list",
			method:'GET',
			success:res=>{
				res.data.data.forEach((item)=>{
					let a={value:item.id,text:item.name}
					range.value.push(a)
				})
			}
		})
		
		let user=localStorage.getItem("user")
		if(user==null){
			uni.showToast({
				icon:'error',
				title:'请先登录'
			})
			setTimeout(() => {
				uni.switchTab({
					url: "/pages/home/home"
				})
			}, 500)
		}
		else{
			userInfo=user;
		}
		
		if(option.bookId!=null){
			bookId=option.bookId
			requestDetail()
		}
		
	})
	
	function requestDetail(){
		let Authorization=localStorage.getItem("Authorization")
		let user = localStorage.getItem("user")
		let userId = JSON.parse(user).id
		uni.request({
			url: baseUrl + '/book/detail',
			header: {
				'Authorization': Authorization
			},
			data: {
				id:bookId
			},
			success: (res) => {
				fillData(res.data)
			}
		})
	}
	
	function fillData(data) {
		name.value = data.name
		desc.value = data.desc
		selectImage.value=[{
			url: data.imagePath,
			extname: 'png',
			name: 'imageName.png'
		}]
		selectCategory.value=data.categoryId
		imageId=data.imageId
	}
</script>

<style lang="scss" scoped>

</style>
